package com.delaphone.flocash.web.action;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;

/**
 * 
 * @author dongtv
 */
public class LogFilter implements Filter {

    private Logger log;

    public void init(FilterConfig filterConfig) throws ServletException {
        log = Logger.getLogger("LogFilter");
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        HttpSession session = req.getSession();
        if (session.isNew() || session.getAttribute("user") == null) {
            try {
                res.sendRedirect("/pub/login.htm");
            } catch (Exception ex1) {
                try {
                    res.sendRedirect("/pub/login.htm");
                } catch (Exception ex2) {
                }
            }
        }
        try {
            chain.doFilter(request, response);
        } catch (Exception ex) {
            log.info(ex.getMessage());
        }
    }

    public void destroy() {
    }
}
